import {shopcarDetail} from '@/network/shopcar'
import { getLocal } from '@/utils/cookie'

const state = {
  shopCartNums: 0,
  shopCart: [],
  token: getLocal('raySaleToken') || '',
}

const mutations = {
  SET_SHOPCARTNMES: (state, shopCartNums) => {
    state.shopCartNums = shopCartNums
  },
  SET_SHOPCART: (state, shopCart) => {
    state.shopCart = shopCart
  },
  SET_GETTOKEN:(state, token)=>{
    state.token = token
  }
}

const actions = {
  getShopCartNums ({ commit }) {
    return new Promise(resolve => {
      shopcarDetail().then(res => {
        if (res.code === 200) {
          const result = res.result
          commit('SET_SHOPCART', result.cartitems)
          commit('SET_SHOPCARTNMES', result.cartitems.length)
          resolve()
        }
      })
    })
  }
}

export default {
  state,
  mutations,
  actions,
  namespaced: true
}




